package im.dhc.gitdiff.common;

import im.dhc.gitdiff.util.FileUtil;
import im.dhc.gitdiff.util.PathUtil;
import im.dhc.gitdiff.util.PropertityUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

/**
 * @author songhaichao
 * @create 2020-06-09-10:05
 **/
public abstract class AbstractDiffHandler implements DiffHandler{

    protected String oldPath;
    protected String newPath;

    protected String projectName;
    protected String onlineDate;
    protected String productDir;
    protected String backupDir;
    protected String updateDir;
    protected String localTargetDir;
    protected String localUpdateDir;

    protected BufferedWriter backupbw;
    protected BufferedWriter updatebw;
    protected BufferedWriter recoverbw;

    public AbstractDiffHandler(){

    }

    public AbstractDiffHandler(String oldPath, String newPath,BufferedWriter backupbw,BufferedWriter updatebw,BufferedWriter recoverbw) throws Exception {
        this.oldPath = PathUtil.getPath(oldPath);
        this.newPath = PathUtil.getPath(newPath);
        this.backupbw = backupbw;
        this.updatebw = updatebw;
        this.recoverbw = recoverbw;
        PropertityUtil PropertityUtil = new PropertityUtil();
        this.projectName = PropertityUtil.getProperty("projectName");
        this.onlineDate = PropertityUtil.getProperty("onlineDate");
        this.productDir = PropertityUtil.getProperty("productDir");
        this.backupDir = PropertityUtil.getProperty("backupDir");
        this.updateDir = PropertityUtil.getProperty("updateDir");
        this.localTargetDir = PropertityUtil.getProperty("localTargetDir");
        this.localUpdateDir = PropertityUtil.getProperty("localUpdateDir");
    }

    protected String backUp(){
        if(oldPath == null) {return null;}
        StringBuilder shell = new StringBuilder();
        shell.append("mkdir -p ");
        shell.append(backupDir);
        shell.append("/");
        shell.append(onlineDate);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath.substring(0,oldPath.lastIndexOf("/")));
        shell.append(" && ");
        shell.append("cp -p ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath);
        shell.append(" ");
        shell.append(backupDir);
        shell.append("/");
        shell.append(onlineDate);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath);

        return shell.toString();
    }

    protected String update(){
        if(newPath == null) {return null;}
        StringBuilder shell = new StringBuilder();
        shell.append("mkdir -p ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(newPath.substring(0,newPath.lastIndexOf("/")));
        shell.append(" && ");
        shell.append("cp -p ");
        shell.append(updateDir);
        shell.append("/");
        shell.append(onlineDate);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(newPath);
        shell.append(" ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(newPath);
        return shell.toString();
    }

    protected String recover(){
        if(oldPath == null){return null;}
        StringBuilder shell = new StringBuilder();
        shell.append("mkdir -p ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath.substring(0,oldPath.lastIndexOf("/")));
        shell.append(" && ");
        shell.append("cp -p ");
        shell.append(backupDir);
        shell.append("/");
        shell.append(onlineDate);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath);
        shell.append(" ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(oldPath);
        return shell.toString();
    }

    protected String delete(String path){
        if(path == null){return null;}
        StringBuilder shell = new StringBuilder();
        shell.append("rm -f ");
        shell.append(productDir);
        shell.append("/");
        shell.append(projectName);
        shell.append("/");
        shell.append(path);
        return shell.toString();
    }

    protected void copyFile() throws IOException {
        String sourcePath = localTargetDir+"/"+newPath;
        String targetPath = localUpdateDir+"/"+onlineDate+"/"+projectName+"/"+newPath;

        File sourceFile = new File(sourcePath);
        FileUtil.makeDirs(targetPath.substring(0,targetPath.lastIndexOf("/")));
        File targetFile = new File(targetPath);

        FileUtil.copyFile(sourceFile,targetFile);
    }
}
